package net.taviscaron.drivenotes.android.activity;

import net.taviscaron.drivenotes.android.R;
import net.taviscaron.drivenotes.android.service.PlacesService;
import net.taviscaron.drivenotes.shared.model.entity.Place;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class PlacesActivity extends ListActivity {
    private static final String PLACE_ACTIVITY = "net.taviscaron.drivenotes.android.PLACE";
    private PlacesListAdapter adapter;
    private PlacesService placesService;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapter = new PlacesListAdapter();
        setListAdapter(adapter);
        getListView().setOnItemClickListener(new OnItemClickListener() {
	    @Override
	    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
		showPlace(position);
	    }
	});
        
        placesService = new PlacesService(this);
    }
    
    @Override
    protected void onStart() {
	super.onStart();
	if(adapter.getPlaces() == null) {
	    if(placesService.isOnline()) {
		new PlacesLoader().execute();
	    } else {
		Toast.makeText(this, "You need an internet connection", Toast.LENGTH_SHORT).show();
	    }
	}
    }
    
    private void showPlace(int position) {
	Place place = adapter.getItem(position);
	Intent intent = new Intent(PLACE_ACTIVITY);
	intent.putExtra("lat", place.getLat());
	intent.putExtra("lon", place.getLon());
	startActivity(intent);
    }
    
    private class PlacesListAdapter extends BaseAdapter {
	private Place[] places;
	
	public void setPlaces(Place[] places) {
	    this.places = places;
	    notifyDataSetChanged();
	}
	
	public Place[] getPlaces() {
	    return places;
	}
	
	@Override
	public int getCount() {
	    return (places != null) ? places.length : 0;
	}

	@Override
	public Place getItem(int position) {
	    return places[position];
	}

	@Override
	public long getItemId(int position) {
	    return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup viewGroup) {
	    if(view == null) {
		LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view = layoutInflater.inflate(R.layout.places_item, null);
	    }
	    
	    Place place = getItem(position);
	    
	    ((TextView)view.findViewById(R.id.placeName)).setText(place.getName());
	    ((TextView)view.findViewById(R.id.placeLatitude)).setText(place.getLat().toString());
	    ((TextView)view.findViewById(R.id.placeLongitude)).setText(place.getLon().toString());
	    ((TextView)view.findViewById(R.id.placeDescription)).setText(place.getDescription());
	    
	    return view;
	}
    }
    
    private class PlacesLoader extends AsyncTask<Void, Void, Place[]> {
	private ProgressDialog dialog;
	@Override
	protected Place[] doInBackground(Void... args) {
	    return placesService.loadPlaces();
	}
	
	@Override
	protected void onPreExecute() {
	    dialog = ProgressDialog.show(PlacesActivity.this, "", "Loading...");
	}
	
	@Override
	protected void onPostExecute(Place[] result) {
	    dialog.dismiss();
	    if(result == null || result.length == 0) {
		Toast.makeText(PlacesActivity.this, "There is no places", Toast.LENGTH_LONG).show();
	    } else {
		adapter.setPlaces(result);
	    }
	}
    }
}
